using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;

public partial class Logout : System.Web.UI.Page
{
    security hdds;
    string sRedirect = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        this.Title = "MaxBlox";
        setPageTitle();
        if (Request.QueryString["EX"] != null)
            if (Request.QueryString["EX"] == "0")
                lbl_msg.Text = "Your Session has expired.";
        
        // Added by srivatsan on June 22 2009
        if (!Page.IsPostBack)
        {
            RemoveSessions();
            //code modified by Sampath on 07-Jan-2011 for session expiry
            if (Request.QueryString["LG"] != null)
            {
                string logoutUrl = Request.QueryString["LG"].ToString();
                if (logoutUrl.StartsWith("http"))
                    Response.Redirect(logoutUrl, true);
                else
                    alogin.HRef = logoutUrl;
            }
            else
            {
                // Added by 'Imran Gani' 13-Mar-2013, to get login page from web config
                alogin.HRef = ConfigurationManager.AppSettings["LoginPage"];
                //alogin.Visible = false;
            }
        }
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {


    }
    protected void lb_login_again_Click(object sender, EventArgs e)
    {

        Response.Redirect(Request.QueryString["LG"].ToString());//Response.Redirect("Default.aspx");
        

        
        //string VirtualPath = Request.Path.Substring(0, Request.Path.LastIndexOf("/"));
        //VirtualPath = VirtualPath.Substring(0, VirtualPath.LastIndexOf("/") + 1);
        //Response.Redirect(VirtualPath + "ecommission/login.aspx");
    }

    private void RemoveSessions()  //Added by srivatsan to prevent postback on logout
    {
        deleteCustomLogo();
        string sRedirect = "";
        hdds = new security();
        
        AuditLog AL = new AuditLog();
        AL.updateUserInCompany();
        AL.updateUserInModel(); // Added by 'Imran Gani' on 15-Sep-2014, to log logout time in Model DB
        AL = null;
        if (!Page.User.Identity.IsAuthenticated)
        {
            if (Page.Request.Cookies["ASP.NET_SessionId"] != null)
            {
                Response.Cookies["ASP.NET_SessionId"].Expires = DateTime.Now.AddYears(-30);
            }


            for (int i = 0; i < Session.Count; i++)
            {
                Session[i] = null;
            }
            LogWriter.WriteLog("Session End : " + DateTime.Now.ToString());
            Session.Abandon();
        }
    }

    private void deleteCustomLogo()
    {
        try
        {
            if (Session["company_afrowid"] != null)
            {
                DirectoryInfo di = new DirectoryInfo(Server.MapPath(".") + "\\Images\\CustomLogo");
                FileInfo[] fis = di.GetFiles(Session["company_afrowid"].ToString() + "*");
                if (fis.Length > 0)
                {
                    File.Delete(fis[0].FullName);
                }
            }
        }
        catch (Exception e)
        {
            LogWriter.WriteLog("Error in deleteCustomLogo: " + e.Message);
        }
        finally
        {

        }
    }
    private void setPageTitle()
    {
        try
        {
            if (Session["pageTitle"] != null)
            {
                Page.Header.Title = Session["pageTitle"].ToString();
            }
            else
            {
                if (System.IO.File.Exists(HttpContext.Current.Server.MapPath(".") + "\\maxbloxtitle.ini"))
                {
                    System.IO.StreamReader sr = new System.IO.StreamReader(HttpContext.Current.Server.MapPath(".") + "\\maxbloxtitle.ini");
                    Session["pageTitle"] = sr.ReadLine();
                    Page.Header.Title = Session["pageTitle"].ToString();
                    sr.Close();
                    sr = null;
                }
            }
        }
        catch (Exception e)
        {
            LogWriter.WriteLog("Error in setPageTitle function. Error: " + e.Message);
        }
    }
}
